﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Manage_Pupils.Controller;
using System.Data.SqlClient;
namespace Manage_Pupils
{
    public partial class FormCapNhatMH : Form
    {
        SqlDataAdapter sqldataAdapter = new SqlDataAdapter();
        DataTable dataTable = new DataTable();
        SqlConnection con;
        public void getData()
        {
            SqlCommand command = new SqlCommand(); // Khai bao 1 command
            command.Connection = con; // Ket noi
            command.CommandType = CommandType.Text; // Khai bao kieu command
            command.CommandText = "Select * from MonHoc";
            sqldataAdapter.SelectCommand = command; // Gan command cho dataAdapter
            sqldataAdapter.Fill(dataTable); // Nap du lieu cho table
        }
        //===========================================================
        public FormCapNhatMH()
        {
            InitializeComponent();
        }
        //===========================================================
        private void Connect()
        {
            try
            {
                String sqlConnect = "Server=IWANTWIN-PC; user id=sa; password=123456; database=QuanLyHocSinh";
                con = new SqlConnection(sqlConnect);
                con.Open(); // Mo ket noi
            }
            catch (Exception e)
            {
                MessageBox.Show("Không thể kết nối tới cơ sở dữ liệu" + e.ToString(), "Error", MessageBoxButtons.OK);
            }
        }
        //=========================================================
        private void DisConnect()
        {
            con.Close(); // Dong ket noi
            con.Dispose(); // Giai phong tai nguyen
            con = null; // Huy doi tuong
        }
        //==========================================================
        private void Themmonhocbutton_Click(object sender, EventArgs e)
        {
            BindingContext[dataTable].AddNew();
            return;




           
        }
        //============================================================
        private void FormCapNhatMH_Load(object sender, EventArgs e)
        {
            Connect();
            getData();
            grvdata.DataSource = dataTable; // Load du lieu len DataGridView
            Binding();
        }
        //===========================================================
        private void Binding()
        {
            textMaMH.DataBindings.Clear();
            textMaMH.DataBindings.Add("Text", grvdata.DataSource, "MaMH");
            textTenMH.DataBindings.Clear();
            textTenMH.DataBindings.Add("Text", grvdata.DataSource, "TenMH");
        }
        //===========================================================
        private void Thoatbutton_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            DisConnect();
        }
        //===========================================================
        private bool check()
        {
            bool kiemtra = true;
            if (textMaMH.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập Mã môn học !", "Thông báo", MessageBoxButtons.OK);
                kiemtra = false;
            }
            if (textTenMH.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập Tên môn học !", "Thông báo", MessageBoxButtons.OK);
                kiemtra = false;
            }
            return kiemtra;
        }
        //===========================================================
        private void Luuthaydoibutton_Click(object sender, EventArgs e)
        {
            check();
            if (check() == true)
            {
                SqlCommand commandInsert = new SqlCommand();
                commandInsert.Connection = con;
                commandInsert.CommandType = CommandType.Text;
                commandInsert.CommandText = @"Insert MonHoc(MaMH,TenMH) values (@MaMH,@TenMH)";
                commandInsert.Parameters.Add("@MaMH", SqlDbType.NChar, 10).Value = textMaMH.Text;
                commandInsert.Parameters.Add("@TenMH", SqlDbType.NChar, 10).Value = textTenMH.Text;
                try
                {
                    if (commandInsert.ExecuteNonQuery() > 0)
                    {
                        MessageBox.Show("Môn học được thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        dataTable.Clear();
                        getData();
                        grvdata.DataSource = dataTable; // Load du lieu len DataGridView
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Lỗi trùng mã môn học", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void Xoamonhocbutton_Click(object sender, EventArgs e)
        {

           int index = this.BindingContext[dataTable].Position;
           this.BindingContext[dataTable].RemoveAt(index);
           SqlCommand commandDelete = new SqlCommand();
           commandDelete.Connection = con;
            commandDelete.CommandType = CommandType.Text;
            commandDelete.CommandText = @"Delete from MonHoc where MaMH= @MaMH";
            commandDelete.Parameters.Add("@MaMH", SqlDbType.NChar, 10, "MaMH");
            sqldataAdapter.DeleteCommand = commandDelete;
            try { 
            if (sqldataAdapter.Update(dataTable) > 0)
            {
                MessageBox.Show("Xoa thanh cong!", "Thông báo", MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
                }catch(Exception){
                    MessageBox.Show("Không thể xoá!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
          
                }
        }

        private void Suamonhocbutton_Click(object sender, EventArgs e)
        {
            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = con;
            commandUpdate.CommandType = CommandType.Text;
            commandUpdate.CommandText = @"UPDATE MonHoc set  TenMH = @TenMH where MaMH = @MaMH";
            //commandUpdate.CommandText = @"UPDATE from MonHoc set  TenMH = @TenMH, MaMH = @MaMH";
            commandUpdate.Parameters.Add("@MaMH", SqlDbType.NChar, 10, "MaMH");
            commandUpdate.Parameters.Add("@TenMH", SqlDbType.NChar, 10, "TenMH");
            sqldataAdapter.UpdateCommand = commandUpdate;
           try
            {
                BindingContext[dataTable].EndCurrentEdit();
                if (sqldataAdapter.Update(dataTable) > 0)
                {
                    MessageBox.Show("Sửa thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else MessageBox.Show("Không thể sửa!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception)
            {
                MessageBox.Show("Không thể sửa!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

           }
        }
    }
}
